﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Microsoft.Modeling;

namespace yoADO
{
    public class AbstractAsync
    {
        public static bool _hasIntialized = false;
        public static bool _hasCreatedConnection = false;
        public static bool _hasCommandReady = false;
        public static bool _hasOpenConnection = false;
        public static bool _hasStartedQuery = false;
        public static bool _hasCompletedQuery = false;

        public static String _connectionString;
        public static String _commandText;
        public static int _count = 0;

        public static SqlConnection connection;
        public static SqlCommand command;
        public static SqlDataReader reader;
        public static IAsyncResult result;

        public static bool initialize()
        {
            //_connectionString = "Data Source=web.ankitbuti.com;Initial Catalog=Northwind;Persist Security Info=True;User ID=yoADO;Password=cs490;Asynchronous Processing=true";
            _connectionString = "Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=yoADO;Password=cs490;Asynchronous Processing=true";
            _commandText = "WAITFOR DELAY '00:00:03'; SELECT [OrderID], [EmployeeID], [OrderDate], [CustomerID], [RequiredDate], [ShippedDate], [ShipAddress] FROM [Orders]";
            _hasIntialized = true;
            return true;
        }

        public static bool createConnection()
        {
            if (_hasIntialized == true)
            {
                connection = new SqlConnection(_connectionString);
                if (connection == null)
                {
                    return false;
                }
                _hasCreatedConnection = true;
                return true;
            }
            return false;
        }

        public static bool createCommand()
        {
            if (_hasCreatedConnection == true)
            {
                command = new SqlCommand(_commandText, connection);
                if (command != null)
                {
                    return false;
                }
                _hasCommandReady = true;
                return true;
            }
            return false;
        }

        public static bool openConnection()
        {
            if (_hasCommandReady == true)
            {
                try
                {
                    connection.Open();
                }
                catch (InvalidOperationException ex)
                {
                    return false;
                }
                catch (SqlException ex)
                {
                    return false;
                }
                catch (ArgumentException ex)
                {
                    return false;
                }
                _hasOpenConnection = true;
                return true;

            }
            return false;
        }

        public static bool runQuery()
        {

            if (_hasOpenConnection == true)
            {
                try
                {
                    result = command.BeginExecuteReader();
                    _hasStartedQuery = true;

                    worker();

                    SqlDataReader reader = command.EndExecuteReader(result);
                    _hasCompletedQuery = true;

                    displayResults();
                }

                catch (SqlException ex)
                {
                    return false;
                }
                catch (InvalidOperationException ex)
                {
                    return false;
                }
                return true;
            }
            return false;

        }

        public static void worker()
        {
            while (!result.IsCompleted)
            {
                updateCounter();
            }
        }

        public static void updateCounter()
        {
            System.Threading.Thread.Sleep(1);
            _count += 1;
        }

        public static void displayResults()
        {
            reader.ToString();
        }

    }
}
